package com.sevici;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class LocationLocaBici extends MyLocationOverlay implements LocationListener  {
	
	private MapView mapView;
	private MapController mapControler;
	private LocationManager lm; 
	private GeoPoint p;
	private Context context;
	
	public LocationLocaBici(Context context, MapView mapView) {
		super(context, mapView);
		this.context = context;
		this.mapView = mapView;
		this.mapControler = mapView.getController();
		lm =(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 50.0f, this);		
	}
	
	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			
			this.p = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
			//mapControler.animateTo(p);
			mapControler.setCenter(p);
		}
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
		if(p != null){
			Paint paint = new Paint();
			
			super.draw(canvas, mapView, shadow);
			// Converts lat/lng-Point to OUR coordinates on the screen.
			Point myScreenCoords = new Point();
			mapView.getProjection().toPixels(p, myScreenCoords);
			
			paint.setStrokeWidth(1);
			paint.setARGB(255, 255, 255, 255);
			paint.setStyle(Paint.Style.STROKE);
			
			Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.blue_dot);
			
			canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
			//canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
			return true;
		} else {
			return false;
		}
	}
}
